package com.hst.capacity.facade.rest;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.hst.capacity.application.biz.DriverBiz;
import com.hst.capacity.application.biz.FleetBiz;
import com.hst.capacity.domain.exception.BusinessException;
import com.hst.capacity.domain.model.request.fleet.*;
import com.hst.capacity.domain.model.response.DefaultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;

import static com.hst.capacity.domain.model.response.DefaultVO.success;

/**
 *  服务商模块Controller
 *
 * */
@RequestMapping("/fleet")
@RestController
public class FleetController {

    @Autowired
    private FleetBiz fleetBiz;
    @Autowired
    private DriverBiz driverBiz;

    /**
     * 车队调度账号检索查询
     * @param param
     * @return
     */
    @PostMapping("/dispatch/account/search")
    private DefaultVO dispatchAccountSearch(@RequestBody @Valid FleetUserSearchParam param) {
        return success(fleetBiz.dispatchAccountSearch(param));
    }

    /**
     * 车队调度账号详细
     * @param param
     * @return
     */
    @PostMapping("/dispatch/account/detail")
    private DefaultVO dispatchAccountDetail(@RequestBody Map<String,String> param) {
        String idFleetUser = param.get("idFleetUser");
        if (StrUtil.isEmpty(idFleetUser))
            throw new BusinessException(4001,"车队调度账号ID[idFleetUser]不能为空");
        return success(fleetBiz.dispatchAccountDetail(idFleetUser));
    }

    /**
     * 车队调度账号保存/添加
     * @param param
     * @return
     */
    @PostMapping("/dispatch/account/save")
    private DefaultVO dispatchAccountSave(@RequestBody @Valid FleetUserSaveParam param) {
        return success(MapUtil.builder("idFleetUser",fleetBiz.dispatchAccountSave(param)).build());
    }

    /**
     * 车队检索查询
     * @param param
     * @return
     */
    @PostMapping("/search")
    private DefaultVO search(@RequestBody @Valid FleetSearchParam param) {
        return success(fleetBiz.search(param));
    }

    /**
     * 车队保存/添加
     * @param param
     * @return
     */
    @PostMapping("/save")
    private DefaultVO save(@RequestBody @Valid FleetSaveParam param) {
        return success(MapUtil.builder("idFleet",fleetBiz.save(param)).build());
    }

    /**
     * 车队编辑查询
     * @param param
     * @return
     */
    @PostMapping("/select")
    private DefaultVO select(@RequestBody Map<String,String> param) {
        String idFleet = param.get("idFleet");
        if (StrUtil.isEmpty(idFleet))
            throw new BusinessException(4001,"车队ID[idFleet]不能为空");
        return success(fleetBiz.select(idFleet));
    }

    /**
     * 车队司机列表
     * @param param
     * @return
     */
    @PostMapping("/search/driver")
    private DefaultVO searchDriver(@RequestBody @Valid FleetSearchDriverParam param) {
            return success(fleetBiz.searchDriverBind(param));
    }

    /**
     * 查询司机
     * @param param
     * @return
     */
    @PostMapping("/search/driver/mobile")
    private DefaultVO searchDriverMobile(@RequestBody @Valid FleetSearchDriverParam param) {
        return success(fleetBiz.searchDriverMobile(param));
    }

    /**
     * 车队数量
     * @param param
     * @return
     */
    @PostMapping("/franchisee/count")
    private DefaultVO fleetCount(@RequestBody Map<String,String> param) {
        String idFranchisee = param.get("idFranchisee");
        if (StrUtil.isEmpty(idFranchisee))
            throw new BusinessException(4001,"运力商ID[idFranchisee]不能为空");
        return success(fleetBiz.fleetCount(idFranchisee));
    }

    /**
     * 司机数量
     * @param param
     * @return
     */
    @PostMapping("/driver/count")
    private DefaultVO fleetDriverCount(@RequestBody Map<String,String> param) {
        String idFleet = param.get("idFleet");
        if (StrUtil.isEmpty(idFleet))
            throw new BusinessException(4001,"运力商ID[idFleet]不能为空");
        return success(driverBiz.fleetDriverCount(idFleet));
    }

    /**
     * 车队司机列表
     * @param param
     * @return
     */
    @PostMapping("/driver/list/unbind")
    private DefaultVO unbindDriverList(@RequestBody @Valid FleetSearchDriverParam param) {
        return success(fleetBiz.searchUnBindFleetDriver(param));
    }

    /**
     * 服务商开启
     * @param param
     * @return
     */
    @PostMapping("/status/open")
    private DefaultVO statusOpen(@RequestBody @Valid FleetStatusParam param) {
        return success(fleetBiz.statusOpen(param));
    }

    /**
     * 服务商冻结
     * @param param
     * @return
     */
    @PostMapping("/status/freeze")
    private DefaultVO statusFreeze(@RequestBody @Valid FleetStatusParam param) {
        return success(fleetBiz.statusFreeze(param));
    }

    /**
     * 车队绑定司机
     * @param param
     * @return
     */
    @PostMapping("/driver/bind")
    private DefaultVO driverBindToggle(@RequestBody @Valid FleetBindDriverParam param) {
        return success(fleetBiz.driverBindToggle(param));
    }

    /**
     * 车队绑定司机(运力商操作)
     * @param param
     * @return
     */
    @PostMapping("/driver/bind/byfranchisee")
    private DefaultVO driverBindByFranchisee(@RequestBody @Valid FleetBindDriverParam param) {
        return success(fleetBiz.driverBindByFranchisee(param));
    }

    /**
     * 车队解绑司机
     * @param param
     * @return
     */
    @PostMapping("/driver/unbind")
    private DefaultVO driverUnBindToggle(@RequestBody @Valid FleetBindDriverParam param) {
        return success(fleetBiz.driverUnBindToggle(param));
    }

    /**
     * 车队绑定司机(运力商操作)
     * @param param
     * @return
     */
    @PostMapping("/driver/unbind/byfranchisee")
    private DefaultVO driverUnBindByFranchisee(@RequestBody @Valid FleetBindDriverParam param) {
        return success(fleetBiz.driverUnBindByFranchisee(param));
    }

    /**
     * 车队分成比例展示
     * @param param
     * @return
     */
    @PostMapping("/commision/select")
    private DefaultVO commisionSelect(@RequestBody Map<String,String> param) {
        String idFleet = param.get("idFleet");
        if (StrUtil.isEmpty(idFleet))
            throw new BusinessException(4001,"车队ID[idFleet]不能为空");
        return success(fleetBiz.commisionSelect(idFleet));
    }

    /**
     * 车队分成比例保存
     * @param param
     * @return
     */
    @PostMapping("/commision/save")
    private DefaultVO commisionSave(@RequestBody @Valid FleetCommisionSaveParam param) {
        return success(MapUtil.builder("idFleetCommision",fleetBiz.commisionSave(param)).build());
    }

    /**
     * 车队分成比例编辑查询
     * @param param
     * @return
     */
    @PostMapping("/commision/save/select")
    private DefaultVO commisionSaveSelect(@RequestBody Map<String,String> param) {
        String idFleetCommision = param.get("idFleetCommision");
        if (StrUtil.isEmpty(idFleetCommision))
            throw new BusinessException(4001,"分成比例ID[idFleetCommision]不能为空");
        return success(fleetBiz.commisionSaveSelect(idFleetCommision));
    }

}
